<template>
	<view class="confirm-modal">
		<u-modal 
			v-model="show" 
			width="560rpx"
			:show-title="false"
			show-cancel-button
			:cancel-style="{color:'#333333', fontSize: '36rpx',borderRight: '1px solid #eee'}"
			:confirm-style="{color:'#19B1B8', fontSize: '36rpx'}"
			:async-close="true"
			@confirm="confirm"
		>
			<view class="confirm-content">
				<view class="title">
					<slot name="title">提示</slot>
				</view>
				<view class="txt">
					<slot name="content" :config="config">确认此操作吗？</slot>
				</view>
			</view>
		</u-modal>
	</view>
</template>
<script>
	export default {
		props: {
			callback: {
				required: true
			}
		},
		data (){
			return {
				// 需要订阅的消息模板的id的集合
				msgTmpIds: ['nymhPXWOaH9118Ee5WW6kvW45V8wCX95EzG5gv3OAJ4', 'dc0AqSF8GWmEDDdX5mnPKlaXUS9vBDD3YAKe1dRStis'],
				show: false,
				config: {},
				executeFn: null
			}
		},
		beforeCreate (){
			// 微信小程序中，只在此生命周期才生效
			this.$eventBus.$on('confirm', (res) => {
				const { data, executeFn } = res;
				this.show = true;
				this.config = Object.assign({}, data);
				this.executeFn = (typeof executeFn === 'function' && executeFn) || function (){ console.log('请传入confirm执行函数') }
			})
		},
		methods: {
			confirm (){
				this.executeFn().then(result => {
					if(result){
						this.callback();
						// #ifndef MP-WEIXIN
						uni.reLaunch({ url: '/pages/mytask/index' });
						// #endif
						// #ifdef MP-WEIXIN
						// 微信小程序订阅消息
						uni.requestSubscribeMessage({
						  tmplIds: this.msgTmpIds,
						  success (res) {
							  uni.reLaunch({ url: '/pages/mytask/index' })
						  }
						})
						// #endif
					}
				});
				setTimeout(() => {
					this.show = false
				}, 1500)
			}
		}
	}
</script>
